<?php

namespace App\Http\Controllers\Sample;

use App\Helpers\Code;
use App\Models\Sample\Sample;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class SampleController extends Controller
{

    /**
     * @author lcy
     */
    public function __construct()
    {
        $this->_validators();
        $this->model = new Sample();
    }

    /**
     * 添加样本
     * @param Request $request
     * @return array
     */
    public function addSample(Request $request)
    {
        try {
            $data = $request->only(array_merge($this->model->getFillable(),['sample_inout_record_id']));
            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->addSample($data);
            $this->checked($model, [
                Code::INSIDE_ERROR => 'sample.sample.family_member_non_exist',
                Code::INSIDE_ERROR_1 => 'sample.sample.type_non_exist',
                Code::INSIDE_ERROR_2 => 'sample.sample.method_non_exist',
                Code::INSIDE_ERROR_3 => 'sample.sample.person_non_exist',
                Code::INSIDE_EXIST_DEPENDENCE => 'sample.sample.doctor_non_exist',
            ]);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 删除样本
     * @param $id
     * @return bool
     */
    public function deleteSample($id){
        try {
            $model = $this->model->deleteSample($id);
            $this->checked($model, ['null' => 'common.delete_failure']);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑样本
     * @param Request $request
     * @return array
     */
    public function editSample(Request $request, $id)
    {
        try {
            $data = $request->only(array_merge($this->model->getFillable(), ['created_by', 'data_scope']));
            $this->rules['state'] = ['bail', 'required', 'in:0,1'];
            $this->rules['bar_code'] = ['bail', 'required', 'max:200', \Illuminate\Validation\Rule::unique('samples')->ignore($id)];
            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editSample($data, $id);
            $this->checked($model, [
                'null' => 'common.get_failure',
                Code::INSIDE_ERROR => 'sample.sample.family_member_non_exist',
                Code::INSIDE_ERROR_1 => 'sample.sample.type_non_exist',
                Code::INSIDE_ERROR_2 => 'sample.sample.method_non_exist',
                Code::INSIDE_ERROR_3 => 'sample.sample.person_non_exist',
                Code::INSIDE_EXIST_DEPENDENCE => 'sample.sample.doctor_non_exist',
            ]);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取指定样本
     * @param $id
     * @return array
     */
    public function getSample($id){
        try {
            $model = $this->model->getSample($id);
            $this->checked($model, ['null' => 'common.get_failure']);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取样本列表
     * @param Request $request
     * @return array
     */
    public function getSampleList(Request $request){
        try {
            $search = $this->handleSearch($request, $this->model);
            $limit = (int)$request->input('limit');
            $sorter = $this->handleSort($request, $this->model->sorters);
            $defaultSorter = $request->input('sorter') ?? 'desc';
            return preprocess($this->model->getSampleList($limit, $sorter, $search, $defaultSorter));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function _validators()
    {
        $this->rules = [
            'family_id' => 'bail|required|numeric',
            'family_member_id' => 'bail|required|numeric',
            'bar_code' => 'bail|required|max:200|unique:samples',
            'sample_type' => 'bail|required|numeric',
            'transport_method' => 'max:200',
            'deal_method' => 'max:200',
            'sample_date' => 'nullable|date',
            'sampled_by' => 'max:200',
            'receive_date' => 'nullable|date',
            'received_by' => 'nullable|numeric',
            'salesman' => 'nullable|numeric',
            'organization' => 'nullable|numeric',
            'department' => 'nullable|numeric',
            'doctor' => 'nullable|numeric',
            'order_state' => 'bail|required|in:0,1,2,3',
            'is_normal' => 'bail|required|in:0,1',
            'is_qualified' => 'bail|required|in:0,1',
            'sample_aim' => 'bail|required|max:200',
            'sample_inout_record_id' => 'bail|required|numeric',
        ];

        $this->messages = [
            'family_id.required' => trans('sample.sample.family_id_required'),
            'family_id.numeric' => trans('sample.sample.family_id_numeric'),
            'family_member_id.required' => trans('sample.sample.family_member_id_required'),
            'family_member_id.numeric' => trans('sample.sample.family_member_id_numeric'),
            'bar_code.required' => trans('sample.sample.bar_code_required'),
            'bar_code.max' => trans('sample.sample.bar_code_max'),
            'bar_code.unique' => trans('sample.sample.bar_code_unique'),
            'sample_type.required' => trans('sample.sample.sample_type_required'),
            'sample_type.numeric' => trans('sample.sample.sample_type_numeric'),
            'transport_method.max' => trans('sample.sample.transport_method_max'),
            'deal_method.max' => trans('sample.sample.deal_method_max'),
            'sample_date.date' => trans('sample.sample.sample_date_date'),
            'sampled_by.max' => trans('sample.sample.sampled_by_max'),
            'receive_date.date' => trans('sample.sample.receive_date_date'),
            'received_by.max' => trans('sample.sample.received_by_max'),
            'salesman.numeric' => trans('sample.sample.salesman_numeric'),
            'organization.numeric' => trans('sample.sample.organization_numeric'),
            'department.numeric' => trans('sample.sample.department_numeric'),
            'doctor.numeric' => trans('sample.sample.doctor_numeric'),
            'order_state.required' => trans('sample.sample.order_state_required'),
            'order_state.in' => trans('sample.sample.order_state_in'),
            'is_normal.required' => trans('sample.sample.is_normal_required'),
            'is_normal.in' => trans('sample.sample.is_normal_in'),
            'is_qualified.required' => trans('sample.sample.is_qualified_required'),
            'is_qualified.in' => trans('sample.sample.is_qualified_in'),
            'sample_aim.required' => trans('sample.sample.sample_aim_required'),
            'sample_aim.max' => trans('sample.sample.sample_aim_max'),
            'state.required' => trans('sample.sample.state_required'),
            'state.in' => trans('sample.sample.state_in'),
            'sample_inout_record_id.required' => trans('sample.sample.sample_inout_record_id_required'),
            'sample_inout_record_id.numeric' => trans('sample.sample.sample_inout_record_id_numeric'),

        ];
    }
}
